package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.AmountType;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.AESUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.domain.WdyWithdraw;
import com.ruoyi.system.service.IWdyUserService;
import com.ruoyi.system.service.IWdyWithdrawService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 提现记录 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-12
 */
@Controller
@RequestMapping("/system/wdyWithdraw")
public class WdyWithdrawController extends BaseController
{
    private String prefix = "system/wdyWithdraw";

	@Autowired
	private IWdyWithdrawService wdyWithdrawService;

	@Autowired
	private IWdyUserService wdyUserService;
	@RequiresPermissions("system:wdyWithdraw:view")
	@GetMapping()
	public String wdyWithdraw()
	{
	    return prefix + "/wdyWithdraw";
	}

	/**
	 * 查询提现记录列表
	 */
	@RequiresPermissions("system:wdyWithdraw:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyWithdraw wdyWithdraw)
	{
		startPage();
        List<WdyWithdraw> list = wdyWithdrawService.selectWdyWithdrawList(wdyWithdraw);
		for(WdyWithdraw e : list) {
			QueryWrapper<WdyUser> query = new QueryWrapper<>();
			query.eq("user_id", e.getUserId());
			WdyUser user = wdyUserService.getOne(query);
			if(user != null) {
				e.addExtraData("mobile", user.getUserName());
			}
			if(StringUtils.isNotEmpty(e.getRemark())) {
				e.setRemark(AESUtil.decrypt(e.getRemark()));
			}
		}
		return getDataTable(list);
	}

	/**
	 * 导出提现记录列表
	 */
	@RequiresPermissions("system:wdyWithdraw:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyWithdraw wdyWithdraw)
    {
    	List<WdyWithdraw> list = wdyWithdrawService.selectWdyWithdrawList(wdyWithdraw);
        ExcelUtil<WdyWithdraw> util = new ExcelUtil<WdyWithdraw>(WdyWithdraw.class);
        return util.exportExcel(list, "wdyWithdraw");
    }
	
	/**
	 * 新增提现记录
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存提现记录
	 */
	@RequiresPermissions("system:wdyWithdraw:add")
	@Log(title = "提现记录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyWithdraw wdyWithdraw)
	{
		try {
			wdyWithdrawService.withdraw(wdyWithdraw);
		} catch (Exception e) {
			log.error("添加异常", e);
			return error();
		}
		return success();
	}

	/**
	 * 修改提现记录
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyWithdraw wdyWithdraw = wdyWithdrawService.selectWdyWithdrawById(id);
		if(StringUtils.isNotEmpty(wdyWithdraw.getRemark())) {
			wdyWithdraw.setRemark(AESUtil.decrypt(wdyWithdraw.getRemark()));
		}
		mmap.put("wdyWithdraw", wdyWithdraw);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存提现记录
	 */
	@RequiresPermissions("system:wdyWithdraw:edit")
	@Log(title = "提现记录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyWithdraw wdyWithdraw)
	{
		if(StringUtils.isNotEmpty(wdyWithdraw.getRemark())) {
			wdyWithdraw.setRemark(AESUtil.encrypt(wdyWithdraw.getRemark()));
		}
		return toAjax(wdyWithdrawService.updateWdyWithdraw(wdyWithdraw));
	}
	
	/**
	 * 删除提现记录
	 */
	@RequiresPermissions("system:wdyWithdraw:remove")
	@Log(title = "提现记录", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyWithdrawService.deleteWdyWithdrawByIds(ids));
	}


	/**
	 * 审核提现记录
	 */
	@GetMapping("/confirm/{id}")
	public String confirm(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyWithdraw wdyWithdraw = wdyWithdrawService.selectWdyWithdrawById(id);
		mmap.put("wdyWithdraw", wdyWithdraw);
		return prefix + "/confirm";
	}

	/**
	 * 审核提现记录
	 */
	@RequiresPermissions("system:wdyWithdraw:confirm")
	@Log(title = "提现记录", businessType = BusinessType.UPDATE)
	@PostMapping("/confirm")
	@ResponseBody
	public AjaxResult confirm(WdyWithdraw wdyWithdraw)
	{
		try {
			wdyWithdrawService.withdrawConfirm(wdyWithdraw);
			return success();
		} catch (BusinessException e) {
			return error(e.getMessage());
		} catch (Exception e) {
			return error();
		}
	}

	/**
	 * 审核通过
	 */
	@RequiresPermissions("system:wdyWithdraw:confirm")
	@Log(title = "审核通过", businessType = BusinessType.UPDATE)
	@PostMapping( "/confirmAll")
	@ResponseBody
	public AjaxResult confirmAll(String ids)
	{
		int successCount = 0;
		int failCount = 0;
		String[] idArray = ids.split(",");
		for(String id : idArray) {
			try {
				WdyWithdraw wdyWithdraw = wdyWithdrawService.getById(id);
				wdyWithdraw.setWithdrawStatus(WdyWithdraw.STATUS_SUCCESS);
				wdyWithdrawService.withdrawConfirm(wdyWithdraw);
				successCount++;
			} catch (Exception e) {
				log.error("批量审核通过异常", e);
				failCount++;
			}
		}
		String msg = String.format("成功%s笔，失败%s笔", successCount, failCount);
		return AjaxResult.success(msg);
	}

	@RequiresPermissions("system:wdyWithdraw:view")
	@PostMapping("/getSum")
	@ResponseBody
	public AjaxResult getSum(WdyWithdraw wdyWithdraw)
	{
		AjaxResult success = success();
		putSum(wdyWithdraw, success, "cnnProcess", AmountType.CNN, WdyWithdraw.STATUS_PROCESSING);
		putSum(wdyWithdraw, success, "cnnSuccess", AmountType.CNN, WdyWithdraw.STATUS_SUCCESS);
		putSum(wdyWithdraw, success, "vcProcess", AmountType.VC, WdyWithdraw.STATUS_PROCESSING);
		putSum(wdyWithdraw, success, "vcSuccess", AmountType.VC, WdyWithdraw.STATUS_SUCCESS);
		putSum(wdyWithdraw, success, "vcwProcess", AmountType.VC_WITHDRAW, WdyWithdraw.STATUS_PROCESSING);
		putSum(wdyWithdraw, success, "vcwSuccess", AmountType.VC_WITHDRAW, WdyWithdraw.STATUS_SUCCESS);
		return success;
	}

	private void putSum(WdyWithdraw wdyWithdraw, AjaxResult success, String key, AmountType amountType, int status) {
		wdyWithdraw.setAmountType(amountType.getCode());
		wdyWithdraw.setWithdrawStatus(status);
		BigDecimal value = wdyWithdrawService.getSum(wdyWithdraw);
		success.put(key, value);
	}
}
